package com.hngs.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hngs.dao.CategoryDao;
import com.hngs.exception.BizException;
import com.hngs.info.PageInfo;
import com.hngs.model.Category;
import com.hngs.model.CategoryQuery;
import com.hngs.threadlocal.LocalPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
//业务逻辑层  主要处理dao获取得数据库数据 m  C
@Service
public class CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    //查询所有 包含条件查询
    public List<Category> select(CategoryQuery query){
        if(query.getPageNum()!=null && query.getPageSize()!=null){
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }

        List<Category> categoryList = categoryDao.select(query);
        if(categoryList instanceof  Page){
            //强制类型转换
            Page page=(Page) categoryList;
            long total = page.getTotal();
            LocalPageUtils.set(PageInfo.builder().total(total).build());
        }

        return categoryList;
    }

    public Integer insert(Category category) {

        return  categoryDao.insert(category);
    }

    public Integer update(Category category) {
        return categoryDao.update(category);
    }

    public Integer delete(Integer id){
        return categoryDao.updateStatus(id,0);
    }
}
